package com.sx.pipeline.repos.local.db.dao

import android.util.Log
import androidx.room.*
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import io.reactivex.rxjava3.core.Single
import java.lang.reflect.ParameterizedType

/**
 * @Author: YY
 * @Date: 2022/3/11
 * @Description: 数据库操作层
 */
@Suppress("UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS")
abstract class CYBaseDao<T> {

    /**
     * 添加单个对象
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insert(obj: T): Single<Long>

    /**
     * 添加数组对象数据
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insert(vararg objs: T): Single<LongArray>

    /**
     * 添加对象集合
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insert(dataList: List<T>): Single<List<Long>>

    /**
     * 根据对象中的主键更新（主键是自动增长的，无需手动赋值）
     */
    @Update
    abstract fun update(vararg obj: T): Single<Int>

    /**
     * 更新全部
     */
    @Update
    abstract fun updateList(t : List<T>): Single<Int>

    /**
     * 根据对象中的主键删除（主键是自动增长的，无需手动赋值）
     */
    @Delete
    abstract fun delete(obj: T): Single<Int>

    /**
     * 删除全部
     */
    fun deleteAll(): Single<T> {
        val query = SimpleSQLiteQuery(
            "delete from $tableName"
        )
        return doDeleteAll(query)
    }

    /**
     * 查询删除
     */
    fun doDeleteByParams(key: Any, value: Any): Single<List<T>> {
        val query = SimpleSQLiteQuery(
            "delete from $tableName where $key == '$value'"
        )
        return doDeleteByParams(query)
    }


    /**
     * 查询全部
     */
    fun findAll(): Single<List<T>> {
        val query = SimpleSQLiteQuery(
            "select * from $tableName"
        )
        return doFindAll(query)
    }

    /**
     * 查询全部
     */
    fun findAll(vararg value: Any): Single<List<T>> {
        val query = SimpleSQLiteQuery("SELECT * FROM " +
                "$tableName WHERE ${value[0]} = '${value[1]}'")
        return doFindAll(query)
    }

    /**
     * 查询单个数据
     * TODO: 根据 指定数据
     */
    fun find(vararg value: Any): Single<T> {
        val query = SimpleSQLiteQuery("SELECT * FROM " +
                "$tableName WHERE ${value[0]} = '${value[1]}'")
        return doFind(query)
    }

    @RawQuery
    protected abstract fun doDeleteAll(query: SupportSQLiteQuery): Single<T>

    @RawQuery
    protected abstract fun doDeleteByParams(query: SupportSQLiteQuery): Single<List<T>>

    @RawQuery
    protected abstract fun doFindAll(query: SupportSQLiteQuery): Single<List<T>>

    @RawQuery
    protected abstract fun doFind(query: SupportSQLiteQuery): Single<T>

    /**
     * 获取表名
     */
    private val tableName: String
        get() {
            val clazz = (javaClass.superclass.genericSuperclass as ParameterizedType)
                .actualTypeArguments[0] as Class<*>
            val tableName = clazz.simpleName
            Log.d("ez", "getTableName: -->$tableName")
            return tableName
        }
}